{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9edf3eef-3907-488c-8387-d1ac8a283e9d",
   "metadata": {},
   "source": [
    "# Lesson 1: Building blocks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b8c826c-5b72-46e0-b66a-2bbd32646d2e",
   "metadata": {},
   "source": [
    "Welcome to this course! Just a reminder that LLMs may not always return the same text. As a result, the outputs you see when running the notebook may vary somewhat from those you see in the film. This applies to all of the notebooks.\n",
    "Enjoy the course!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23c14c87-7106-4b50-ad4e-da46364f4538",
   "metadata": {},
   "source": [
    "## Language model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35913328-ff65-4792-8b89-8c35bf30fa6b",
   "metadata": {},
   "source": [
    "1. Text LLMs: string -> string\n",
    "2. Chat models: list of messages -> single message output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "912d74ad-63a2-4ee0-9c83-5ac266269ade",
   "metadata": {},
   "outputs": [],
   "source": [
    "import \"dotenv/config\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6deedbe-764a-4459-96ec-b361f0538a64",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { ChatOpenAI } from \"@langchain/openai\";\n",
    "import { HumanMessage } from \"@langchain/core/messages\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a67ce54c-cc4c-42d6-b6bb-577ca1e8ae5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "const model = new ChatOpenAI({\n",
    "    modelName: \"gpt-3.5-turbo-1106\"\n",
    "});\n",
    "\n",
    "await model.invoke([\n",
    "    new HumanMessage(\"Tell me a joke.\")\n",
    "]);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37bf4917-0eeb-4e68-b530-cfef6c2da529",
   "metadata": {},
   "source": [
    "## Prompt template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38328f44-9a39-40a2-9cb1-3bedf0fe5ff7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
    "\n",
    "const prompt = ChatPromptTemplate.fromTemplate(\n",
    "    `What are three good names for a company that makes {product}?`\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0479f0f-f545-4849-aac0-4c96648105cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "await prompt.format({\n",
    "    product: \"colorful socks\"\n",
    "});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5913982b-7b32-4643-828c-6f2e4e58f86a",
   "metadata": {},
   "outputs": [],
   "source": [
    "await prompt.formatMessages({\n",
    "    product: \"colorful socks\"\n",
    "});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3e586e5-078f-4154-bd05-b7bc73aa1f0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { \n",
    "  SystemMessagePromptTemplate, \n",
    "  HumanMessagePromptTemplate \n",
    "} from \"@langchain/core/prompts\";\n",
    "\n",
    "const promptFromMessages = ChatPromptTemplate.fromMessages([\n",
    "  SystemMessagePromptTemplate.fromTemplate(\n",
    "    \"You are an expert at picking company names.\"\n",
    "  ),\n",
    "  HumanMessagePromptTemplate.fromTemplate(\n",
    "    \"What are three good names for a company that makes {product}?\"\n",
    "  )\n",
    "]);\n",
    "\n",
    "await promptFromMessages.formatMessages({\n",
    "    product: \"shiny objects\"\n",
    "});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44e6ed68-1456-4e4f-a64c-f87034153ff0",
   "metadata": {},
   "outputs": [],
   "source": [
    "const promptFromMessages = ChatPromptTemplate.fromMessages([\n",
    "  [\"system\", \"You are an expert at picking company names.\"],\n",
    "  [\"human\", \"What are three good names for a company that makes {product}?\"]\n",
    "]);\n",
    "\n",
    "await promptFromMessages.formatMessages({\n",
    "  product: \"shiny objects\"\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcea2720-7050-4966-84bc-676e7dbb887b",
   "metadata": {},
   "source": [
    "# LangChain Expression Language (LCEL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bf52de8-dbb6-4854-8b9f-500f67345481",
   "metadata": {},
   "outputs": [],
   "source": [
    "const chain = prompt.pipe(model);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fed056d8-cab8-41ac-afaa-7fea3953bbb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "await chain.invoke({\n",
    "    product: \"colorful socks\"\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a362b6b-f72e-46ab-a2e3-4f5ff1f96a10",
   "metadata": {},
   "source": [
    "## Output parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a056699d-bc4f-4668-aaa7-195c3b2a60e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { StringOutputParser } from \"@langchain/core/output_parsers\";\n",
    "\n",
    "const outputParser = new StringOutputParser();\n",
    "\n",
    "const nameGenerationChain = prompt.pipe(model).pipe(outputParser);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2690f628-6e53-45c0-9795-0cbd7424874d",
   "metadata": {},
   "outputs": [],
   "source": [
    "await nameGenerationChain.invoke({\n",
    "    product: \"fancy cookies\"\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e7ece76-e8ce-48a7-b15a-b49687d7d8d3",
   "metadata": {},
   "source": [
    "https://smith.langchain.com/public/9fd7689f-23c6-4ea1-8a76-fea1b5f8d5db/r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2acb9f1f-2fdb-40f3-9eb9-073ff18b0526",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { RunnableSequence } from \"@langchain/core/runnables\";\n",
    "\n",
    "const nameGenerationChain = RunnableSequence.from([\n",
    "    prompt,\n",
    "    model,\n",
    "    outputParser\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57d444e0-f2ca-4566-8143-d9c1ef1a9a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "await nameGenerationChain.invoke({\n",
    "    product: \"fancy cookies\"\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5152045a-3091-4a4a-861f-7ca1cbd61c77",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3eaa073-d377-4794-888f-936a7d982e1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "const stream = await nameGenerationChain.stream({\n",
    "  product: \"really cool robots\",\n",
    "});\n",
    "\n",
    "for await (const chunk of stream) {\n",
    "    console.log(chunk);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c52a0b54-bc01-4e25-8534-2d8ca2676d18",
   "metadata": {},
   "source": [
    "## Batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "816da7ea-4639-4573-9d86-1fb1c7a57c02",
   "metadata": {},
   "outputs": [],
   "source": [
    "const inputs = [\n",
    "    { product: \"large calculators\" },\n",
    "    { product: \"alpaca wool sweaters\" }\n",
    "];\n",
    "\n",
    "await nameGenerationChain.batch(inputs);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4766b8d6-279d-4506-a870-c622a84f7bfb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bc5d32f-afc1-4197-8e13-d2bff0ef2777",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58225177-787e-4280-ae98-b197ed2cb413",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80526d1f-ae14-4f8a-a91d-fec7b5e698e1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33f7d4f0-b387-453c-8ffd-e64bba4daca8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7530a6d4-f4dc-461e-9d9f-44debddee5af",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f2096cc-ff77-4499-bcc7-e8ba537847a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb08dc7f-2879-47f5-8f9b-1436ff7be8a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "871ec465-29a8-4b88-811a-3711830b6471",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4dbb89c5-f3cf-40da-a40f-c4e62007f7ae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38eef9ab-4239-414e-98ed-76880ad2afa9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Deno",
   "language": "typescript",
   "name": "deno"
  },
  "language_info": {
   "file_extension": ".ts",
   "mimetype": "text/x.typescript",
   "name": "typescript",
   "nb_converter": "script",
   "pygments_lexer": "typescript",
   "version": "5.2.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
